<?php

namespace App\Http\Controllers\OpenShop\Feishu;

use App\Http\Controllers\BaseController;
use App\Http\Validate\OpenShop\InvestmentPromotion\PlaceholderValidate;
use App\Http\Validate\OpenShop\InvestmentPromotion\UserBaseInfoValidate;
use App\Http\Validate\OpenShop\InvestmentPromotion\UserExperienceValidate;
use App\Http\Validate\OpenShop\InvestmentPromotion\UserIdentityCardValidate;
use App\Models\OpenShop\InvestmentPromotion\OpenShopUserBaseInfoModel;
use App\Service\OpenShop\UserBaseInfoService;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;


/**
 * @author Green Leaf <786407418@qq.com>
 */
class UserBaseInfoController extends BaseController
{

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function identityCardRecognize(Request $request): JsonResponse
    {
        $params       = UserIdentityCardValidate::recognizeValidate($request->all());
        $uploadedFile = $request->file('file');
        if (!$uploadedFile instanceof UploadedFile) {
            return $this->fail(0, '请上传文件');
        }
        return $this->success(UserBaseInfoService::identityCardRecognize($uploadedFile, $params['type']));
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function publicIdentityCardRecognize(Request $request): JsonResponse
    {
        $params       = UserIdentityCardValidate::recognizeValidate($request->all());
        $uploadedFile = $request->file('file');
        if (!$uploadedFile instanceof UploadedFile) {
            return $this->fail(0, '请上传文件');
        }
        return $this->success(UserBaseInfoService::publicIdentityCardRecognize($uploadedFile, $params['type']));
    }


    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function businessLicenseRecognize(Request $request): JsonResponse
    {
        $uploadedFile = $request->file('file');
        if (!$uploadedFile instanceof UploadedFile) {
            return $this->fail(0, '请上传文件');
        }
        $expect = (string)$request->get('expect', '');
        if ($expect && !in_array($expect,['ignoreOcrError'])){
            return $this->fail(0, 'expect参数异常');
        }
        return $this->success(UserBaseInfoService::businessLicenseRecognize($uploadedFile, $expect));
    }


    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function insert(Request $request): JsonResponse
    {
        $userIdentityCardInfo = UserIdentityCardValidate::insertValidate($request->all());
        $userBaseInfo         = UserBaseInfoValidate::insertValidate($request->all());
        $userExperience       = UserExperienceValidate::insertValidate($request->all());
        $placeholderInfo      = PlaceholderValidate::insertValidate($request->get('placeholderInfo'));

        return $this->success(UserBaseInfoService::insert(getRmsUserInfoByAuth(), $userIdentityCardInfo,
            $userBaseInfo, $userExperience, [], [], $placeholderInfo));
    }


    /**
     * @param Request $request
     * @param string $orderNo
     * @return JsonResponse
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function update(Request $request, string $orderNo): JsonResponse
    {
        $requestParams        = $request->merge(['orderNo' => $orderNo])->all();
        $userIdentityCardInfo = UserIdentityCardValidate::updateValidate($requestParams);
        $userBaseInfo         = UserBaseInfoValidate::updateValidate($requestParams);
        $userExperience       = UserExperienceValidate::updateValidate($requestParams);
        $placeholderInfo      = PlaceholderValidate::insertValidate($request->get('placeholderInfo'));

        UserBaseInfoService::update(getRmsUserInfoByAuth()->id, $orderNo, $userIdentityCardInfo, $userBaseInfo,
            $userExperience, $placeholderInfo);
        return $this->success();
    }

    /**
     * @param Request $request
     * @param string $orderNo
     * @return JsonResponse
     * @throws ValidationException
     */
    public function detail(Request $request, string $orderNo): JsonResponse
    {
        $requestParams = $request->merge(['orderNo' => $orderNo])->all();
        UserBaseInfoValidate::detailValidate($requestParams);
        return $this->success(UserBaseInfoService::detail($orderNo));
    }

    /**
     * 已支付开店订单修改基础信息
     *
     * @param Request $request
     * @param string $orderNo
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     * @throws Exception
     */
    public function paidOrderBaseInfoUpdate(Request $request, string $orderNo): JsonResponse
    {
        $requestParams = UserBaseInfoValidate::orderBaseInfoUpdateValidate($request->all());
        $remark        = $request->post("remark", "");
        UserBaseInfoService::paidOrderBaseInfoUpdate(getRmsUserInfoByAuth(), $orderNo, $requestParams, $remark);
        return $this->success();
    }
}
